package com.cenxi.common.result;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.util.Collections;
import java.util.List;

/**
 * 分页请求结果
 *
 * @author _
 * @date 2020-08-24
 */
@SuperBuilder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResp<T> {

    private static final long serialVersionUID = 1L;

    /**
     * 分页数据
     */
    private List<T> data;

    /**
     * 总页数
     */
    private long totalPage;

    /**
     * 总条数
     */
    private long totalCount;

    /**
     * 是否有下一页
     */
    private boolean hasNext;

    static final String RES_MSG = "成功";
    static final String RES_CODE = "0000";
    @Builder.Default
    private String msg = RES_MSG;
    @Builder.Default
    private String code = RES_CODE;

    public static <T> PageResp<T> error() {
        return PageResp.<T>builder()
                .msg("网络波动").code("0001").hasNext(false).totalPage(0)
                .totalCount(0).data(Collections.emptyList())
                .build();
    }

    public static <T> PageResp<T> error(String msg, String code) {
        return PageResp.<T>builder()
                .msg(msg).code(code).hasNext(false).totalPage(0)
                .totalCount(0).data(Collections.emptyList())
                .build();
    }

    public static <T> PageResp<T> error(String msg) {
        return PageResp.<T>builder()
                .msg(msg).code("0001").hasNext(false).totalPage(0)
                .totalCount(0).data(Collections.emptyList())
                .build();
    }

    public static <T> PageResp<T> succeed() {
        return PageResp.<T>builder().hasNext(false).totalPage(0)
                .totalCount(0).data(Collections.emptyList())
                .build();
    }

    public static <T> PageResp<T> succeed(long totalPage, long totalCount, boolean hasNext, List<T> data) {
        return PageResp.<T>builder().
                hasNext(hasNext).totalPage(totalPage)
                .totalCount(totalCount).data(data)
                .build();
    }

    public static <T> PageResp<T> succeed(String msg, long totalPage, long totalCount, boolean hasNext, List<T> data) {
        return PageResp.<T>builder().msg(msg).
                hasNext(hasNext).totalPage(totalPage)
                .totalCount(totalCount).data(data)
                .build();
    }

}
